﻿namespace TestTool.GUI.Utils
{
    using System;
    using System.Runtime.CompilerServices;
    using System.Windows.Forms;
    using TestTool.Tests.Definitions.Interfaces;

    internal class Operator : IOperator
    {
        private Form _ownerWindow;

        public Operator(Form ownerWindow)
        {
            this._ownerWindow = ownerWindow;
        }

        private bool GetOkCancel(string question)
        {
            return (MessageBox.Show(this._ownerWindow, question, "Interaction required", MessageBoxButtons.OKCancel) == DialogResult.OK);
        }

        public bool GetOkCancelAnswer(string question)
        {
            IAsyncResult asyncResult = this._ownerWindow.BeginInvoke(new GetAnswerDelegate(this.GetOkCancel), new object[] { question });
            return (bool) this._ownerWindow.EndInvoke(asyncResult);
        }

        private bool GetYesNo(string question)
        {
            return (MessageBox.Show(this._ownerWindow, question, "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes);
        }

        public bool GetYesNoAnswer(string question)
        {
            IAsyncResult asyncResult = this._ownerWindow.BeginInvoke(new GetAnswerDelegate(this.GetYesNo), new object[] { question });
            return (bool) this._ownerWindow.EndInvoke(asyncResult);
        }

        private delegate bool GetAnswerDelegate(string question);
    }
}

